package com.duke.shaking.widget;

import java.util.List;

import com.duke.shaking.R;
import com.duke.shaking.pojo.UserPhoto;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class PhotoGridAdapter extends BaseAdapter {

	private List<UserPhoto> photos;
	private LayoutInflater inflater;
	private Context context;
	private int photoWidth;
	private int photoHeight;
	
	public PhotoGridAdapter(Context context, List<UserPhoto> photos) {
		this.context = context;
		this.photos = photos;
		photoWidth = 0;
		photoHeight = 0;
		inflater = LayoutInflater.from(context);
	}
	
	public void setPhotoSize(int width, int height) {
		photoWidth = width;
		photoHeight = height;
		Log.i("shaking", "photogrid width: " + width + " ,height: " + height);
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		if (photos == null) {
			return 0;
		}
		Log.i("shaking", "photos count: " + photos.size());
		return photos.size();
	}

	public Object getItem(int position) {
		if (photos == null || position >= photos.size()) {
			return null;
		}
		return photos.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View contentView, ViewGroup parent) {
		
		PhotoViewHolder holder = null;
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.view_photo_listitem, null);
			holder = new PhotoViewHolder();
			holder.img_photo = (ImageView)contentView.findViewById(R.id.img_photo);
			holder.img_photo.setScaleType(ScaleType.CENTER_CROP);
			holder.txt_profile = (TextView)contentView.findViewById(R.id.txt_profile);
			if (photoWidth > 0 && photoHeight > 0) {
				ViewGroup.LayoutParams layout = holder.img_photo.getLayoutParams();
				layout.width = photoWidth;
				layout.height = photoHeight;
				holder.img_photo.setLayoutParams(layout);
				
				layout = holder.txt_profile.getLayoutParams();
				layout.width = photoWidth;
				holder.txt_profile.setLayoutParams(layout);
				Log.i("shaking", "set photo width: " + photoWidth + " ,height: " + photoHeight);
			}
			contentView.setTag(holder);
		}
		else {
			holder = (PhotoViewHolder)contentView.getTag();
		}
		if (holder != null) {
			UserPhoto photo = photos.get(position);
			if (photo.getLocationType() == UserPhoto.LocationType_ASSETS) {
				holder.img_photo.setImageBitmap(photo.loadFromAssets(context));
			}
			holder.txt_profile.setText(photo.getTitle());
		}
		
		return contentView;
	}
	
	private class PhotoViewHolder {
		public ImageView img_photo;
		public TextView  txt_profile;
	}

}
